package com.matrix.geo.playmysqlgeo.serializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.matrix.geo.playmysqlgeo.util.JtsUtil;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Geometry;

import java.io.IOException;

@Slf4j
public class GeometryJacksonDeserializer extends JsonDeserializer<Geometry> {

    @Override
    public Geometry deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
        String geoJsonString = node.toString();
        return JtsUtil.geoJson2Geometry(geoJsonString);
    }

}
